/***
  *****************************************************************************
  * @file		main.c
  * @author			
  * @version	V1.0
  * @date 		2014-11-15
  * @brief 
  *****************************************************************************
 ***/

/** 
  * @name	int main(void)
  * @brief  主函数
  * @param	none
  * @retval	none
  */

#include "oled_spi_drv.h"
#include <wiringPi.h>
#include "aero_menu.h"
#include "aero_key.h"
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>

unsigned char oled_gram[128][8];
spiPinDriver spiPinDrv;
spiPinDriver *pt_spiPinDrv;

Menu *currentMenu;


Menu MainMenu = { //定义主菜单
 0,3,4,0,//默认显示0-3项，总共4项，当前选择第0项
  {
  "1.Setting >",
  "2.Setting >",
  "3.GetValue>",
  "4.About me>"
  }
};

Menu searchMenu = {//查询菜单
 0,3,6,0,
 {
  "41 Test     ",
  "42 me fm    ",
  "设备机号        ",
  "本机IP地址      ",
  "记录空间大小    ",
  "软件版本        "
 }
};

Menu AboutMenu = {//查询菜单
 0,3,4,0,
 {
  "Version:4252",
  "Project     ",
  "设备机号        ",
  "本机IP地址      ",
 }
};


void func(void)
{

printf("hello");
}

//初始化菜单:
void initMenu()
{
 MainMenu.subMenus = malloc(sizeof(MainMenu)*4);
  MainMenu.subMenus[0] = NULL;//第1到3项没有子菜单置null,选择后程序会调用func中相应项中的函数
  MainMenu.subMenus[1] = NULL;
  MainMenu.subMenus[2] = &searchMenu;
	MainMenu.subMenus[3] = &AboutMenu;//第四项查询有子菜单
 MainMenu.func = malloc(sizeof(&func)*4);
  MainMenu.func[0] = NULL;
  MainMenu.func[1] = NULL;
 MainMenu.func[2] = &func;//当选择了并按了确定，会执行func函数
  MainMenu.func[3] = NULL;
  MainMenu.parent = NULL;//表示是顶级菜单
 
 searchMenu.subMenus = malloc(sizeof(&searchMenu)*6);
searchMenu.subMenus[0] = searchMenu.subMenus[1] = searchMenu.subMenus[2] = searchMenu.subMenus[3] = searchMenu.subMenus[4] = searchMenu.subMenus[5] = NULL;
 searchMenu.func = malloc(sizeof(&func)*6);
 searchMenu.func[0] = searchMenu.func[2] = searchMenu.func[2] = searchMenu.func[3] = searchMenu.func[4] = searchMenu.func[5] = NULL;
 searchMenu.parent = &MainMenu;//上一级菜单是MainMenu.进入查询子菜单后按返回键，将会显示这个菜单 项

	AboutMenu.subMenus = malloc(sizeof(&AboutMenu)*4);
	AboutMenu.subMenus[0]=NULL;
	AboutMenu.subMenus[1]=NULL;
	AboutMenu.subMenus[2]=NULL;
	AboutMenu.subMenus[3]=NULL;

	AboutMenu.func = malloc(sizeof(&func)*4);
	AboutMenu.func[0]=NULL;
	AboutMenu.func[1]=NULL;
	AboutMenu.func[2]=NULL;
	AboutMenu.func[3]=NULL;
	
	AboutMenu.parent = &MainMenu;

 currentMenu = &MainMenu;
 
}

unsigned char key_up;

int main(void)
{
    //设置wiring lib
    wiringPiSetup();
    //初始化oledGpio;
	aeroOledInitGpio();
    aeroKeyInit();
    pt_spiPinDrv = &spiPinDrv;
    initOledDrv();
	oled_init();
	
	initMenu();
 	aeroDisplayLine(currentMenu->selected,currentMenu);

	 int key;

   oledDisplayOn();
   delay(1000);
   oledDisplayOff();
   delay(1000);
   oledDisplayOn();
   delay(1000);
   
   
	key_up = 1;
		while(1)
		{
			key = aeroKeyScan(1);
				if(key == 0)
				{
					delay(10);
					continue;
				}
			printf("key %d is press",key);
			
			switch(key)
			{
				case 4://向上
					if(currentMenu->selected == 0)//到了第一项
     					break;
    				else
    				{
     				currentMenu->selected--;
     				if(currentMenu->selected < currentMenu->range_from)//更新显示的区域
     				{
      					currentMenu->range_from = currentMenu->selected;
      					currentMenu->range_to = currentMenu->range_from+3;
     				}
     				aeroDisplayLine(currentMenu->selected,currentMenu);
     				break;
    				}
   case 2://向下
    if(currentMenu->selected == currentMenu->itemCount-1)
     break;
    else
    {
     currentMenu->selected++;
     if(currentMenu->selected>currentMenu->range_to)
     {
      currentMenu->range_to = currentMenu->selected;
      currentMenu->range_from = currentMenu->range_to-3;
     }
     aeroDisplayLine(currentMenu->selected,currentMenu);
     break;
    }
   case 1://Enter键
   {
    if(currentMenu->subMenus[currentMenu->selected] !=NULL)
    {
     currentMenu = currentMenu->subMenus[currentMenu->selected];
     aeroDisplayLine(0,currentMenu);
    }
    else
    {
     if(currentMenu->func[currentMenu->selected] != NULL)
     {
      currentMenu->func[currentMenu->selected]();//执行相应的函数
      aeroDisplayLine(currentMenu->selected,currentMenu);//返回后恢复原来的菜单状态
     }
    }
    break;
   }
   case 3://返回键
   {
    if(currentMenu->parent!=NULL)//父菜单不为空，将显示父菜单
    {
     currentMenu = currentMenu->parent;
     aeroDisplayLine(currentMenu->selected,currentMenu);
    }
    break;
   }
   default:
     break;
  }
  delay(10);
 }

}